package com.javacoo.xservice.base.support.lock;

import java.util.concurrent.TimeUnit;

import com.javacoo.xkernel.spi.Spi;
import com.javacoo.xservice.base.support.lock.config.LockConfig;

/**
 * 锁接口
 * <li></li>
 *
 * @author: duanyong@jccfc.com
 * @since: 2020/6/22 10:19
 */
@Spi(LockConfig.DEFAULT_IMPL)
public interface Lock<T> {
    /**超时时间*/
    int TIMEOUT_SECOND = 60;
    /**
     * 对lockKey加锁
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2020/6/22 10:30
     * @param lockKey:lockKey
     * @return: T 锁对象
     */
    T lock(String lockKey);
    /**
     * 对lockKey加锁,timeout后过期
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2020/6/22 10:31
     * @param lockKey: lockKey
     * @param timeout: 锁超时时间，单位：秒
     * @return: T 锁对象
     */
    T lock(String lockKey, int timeout);
    /**
     * 对lockKey加锁,指定时间单位，timeout后过期
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2020/6/22 10:33
     * @param lockKey: lockKey
     * @param unit: 时间单位
     * @param timeout: 锁超时时间
     * @return: T 锁对象
     */
    T lock(String lockKey, TimeUnit unit , int timeout);
    /**
     * 尝试获取锁
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2020/6/22 10:35
     * @param lockKey: lockKey
     * @return: boolean 是否成功,成功返回:true
     */
    boolean tryLock(String lockKey);
    /**
     * 尝试获取锁
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2020/6/22 10:35
     * @param lockKey: lockKey
     * @param waitTime:最多等待时间
     * @param timeout:上锁后自动释放锁时间
     * @return: boolean 是否成功,成功返回:true
     */
    boolean tryLock(String lockKey, int waitTime, int timeout);
    /**
     * 尝试获取锁
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2020/6/22 10:36
     * @param lockKey: lockKey
     * @param unit:时间单位
     * @param waitTime:最多等待时间
     * @param timeout:上锁后自动释放锁时间
     * @return: boolean 是否成功,成功返回:true
     */
    boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int timeout);
    /**
     * 释放锁
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2020/6/22 10:37
     * @param lockKey: lockKey
     * @return: void
     */
    void unlock(String lockKey);
    /**
     * 释放锁
     * <li></li>
     * @author duanyong@jccfc.com
     * @date 2020/6/22 10:37
     * @param lock:锁对象
     * @return: void
     */
    void unlock(T lock);
}
